package com.example.smartcanteen.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.smartcanteen.mapper.AssessMapper;
import com.example.smartcanteen.mapper.GoodsMapper;
import com.example.smartcanteen.mapper.ShopMapper;
import com.example.smartcanteen.model.Assess;
import com.example.smartcanteen.model.Goods;
import com.example.smartcanteen.model.Shop;
import com.example.smartcanteen.utils.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class AssessService {
    @Autowired
    private AssessMapper assessMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private ShopMapper shopMapper;


    //生成新评价
    public ResponseMessage create(Assess assess) {
        ResponseMessage responseMessage = new ResponseMessage();

        //先判断该订单商品之前是否有过评价
        QueryWrapper<Assess> assessQueryWrapper = new QueryWrapper<>();
        assessQueryWrapper.eq("orderID", assess.getOrderID());
        if (assessMapper.selectOne(assessQueryWrapper) == null) {

            //其次判断评分是否符合规定
            if (assess.getScore() > 0.0 && assess.getScore() <= 5) {

                //插入新评价
                assessMapper.insert(assess);
                responseMessage.setSuccess(true);
                responseMessage.setMessage("评价成功！");


            } else {
                responseMessage.setSuccess(false);
                responseMessage.setMessage("输入评分有误！");
            }
        } else {
            responseMessage.setSuccess(false);
            responseMessage.setMessage("已评价过该商品！");
        }
        if (responseMessage.getSuccess()) {

            //实时修改商品评分
            goodsScore(assess.getGoodsID());

            //实时修改店铺评分
            shopScore(assess.getGoodsID());
        }

        return responseMessage;
    }


    //商品总评分更新
    public void goodsScore(int goodsID) {
        double score = 0;

        //查询商品的全部评价
        HashMap<String, Object> map = new HashMap<>();
        map.put("goodsID", goodsID);
        List<Assess> assesses = assessMapper.selectByMap(map);

        //计算评分
        for (Assess assess : assesses) {
            score += assess.getScore();
        }
        ;
        score /= assesses.size();

        //修改商品评分
        UpdateWrapper<Goods> goodsUpdateWrapper = new UpdateWrapper<>();
        goodsUpdateWrapper.set("score", score)
                .eq("id", goodsID);
        goodsMapper.update(goodsUpdateWrapper);


    }


    //店铺总评分更新
    public void shopScore(int goodsID) {
        double score = 0;

        //通过实时修改的商品评分查找对应商家ID
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
        goodsQueryWrapper.eq("id", goodsID);
        Goods goods = goodsMapper.selectOne(goodsQueryWrapper);

        //查找商家全部商品（评分）
        QueryWrapper<Goods> goodsQueryWrapper1 = new QueryWrapper<>();
        goodsQueryWrapper1.eq("shopID", goods.getShopID());
        List<Goods> goodsList = goodsMapper.selectList(goodsQueryWrapper1);

        //计算评分
        for (Goods goods1 : goodsList) {
            score += goods1.getScore();
        }
        ;
        score /= goodsList.size();

        //修改店铺总评分
        UpdateWrapper<Shop> shopUpdateWrapper = new UpdateWrapper<>();
        shopUpdateWrapper.set("scores", score)
                .eq("id", goods.getShopID());
        shopMapper.update(shopUpdateWrapper);
    }
}
